/*
 * bsp_object.h
 *
 * Copyright (C) 2011 - Dr.NP
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @version bsp_object.h 1.0.0
 * @package libbspcore
 * @author Dr.NP <np@bsgroup.org>
 * @update 09/30/2011
 */

#ifndef BSP_OBJECT_H

#define BSP_OBJECT_H                            1

#include "bsp_types.h"
#include "bsp_utils.h"

#include <sys/types.h>
// Definations
#define ITEM_BUCKET_LIST_INITIAL                16
#define ITEM_BUCKET_SIZE                        1048576

#define HASH_SIZE_INITIAL                       8

#define PKG_VALUE_TYPE_BOOL                     0x0
#define PKG_VALUE_TYPE_BYTE                     0x1
#define PKG_VALUE_TYPE_SHORT                    0x2
#define PKG_VALUE_TYPE_INT                      0x3
#define PKG_VALUE_TYPE_LONGLONG                 0x4
#define PKG_VALUE_TYPE_FLOAT                    0x5
#define PKG_VALUE_TYPE_STRING                   0x6
#define PKG_VALUE_TYPE_ARRAY_START              0xF0
#define PKG_VALUE_TYPE_ARRAY_END                0xF1
#define PKG_VALUE_TYPE_NULL                     0xFF
#define PKG_UNSUPPORTED_TYPE_KEY                "__UNSUPPORTED_DATA_TYPE_KEY__"

#define NULL_KEY                                "KEY_NULL"

// Macros

// Structs
struct object_t
{
    size_t nitems;
    size_t hash_size;
    struct kv_t *hash_table[2];
    struct kv_t *head;
    struct kv_t *tail;
    struct kv_t *curr;
    int curr_table;
};

struct var_t
{
    char lval[8];
    void *rval;
    size_t rval_len;
    char type;
};

struct kv_t
{
    char *key;
    size_t key_len;
    long long int key_int;
    struct kv_t *prev;
    struct kv_t *next;
    struct kv_t *lprev;
    struct kv_t *lnext;
    struct var_t value;
};

// Functions
int object_init();
struct object_t * new_object();
void free_object(struct object_t *);
void sort_object(struct object_t *);
void reset_object(struct object_t *);
void rehash_object(struct object_t *, size_t);
void debug_link(struct kv_t **, size_t, struct abuff_t *);
void dump_object(struct object_t *, struct abuff_t *);

struct kv_t * alloc_item(const char *, int);
void insert_item(struct object_t *, struct kv_t *);
struct kv_t * remove_item(struct object_t *, struct kv_t *);
struct kv_t * get_item(struct object_t *, const char *, int);
struct kv_t * curr_item(struct object_t *);
void next_item(struct object_t *);
void prev_item(struct object_t *);

void set_bool_item(struct kv_t *, int);
void set_long_item(struct kv_t *, int64_t);
void set_double_item(struct kv_t *, double);
void set_string_item(struct kv_t *, const char *, int);
void set_object_item(struct kv_t *, struct object_t *);
void set_null_item(struct kv_t *);

void serialize_from_object(struct object_t *, struct abuff_t *);
int unserialize_to_object(const char *, size_t, struct object_t *);

#endif  /* bsp_object.h */
